#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define COMM1 1 << 1
#define COMM2 1 << 2
#define COMM3 1 << 3

//void getComm(int flag)
//{
//  if(COMM1 & flag) { printf("COMM1\n"); }
//  if(COMM2 & flag) { printf("COMM2\n"); }
//  if(COMM3 & flag) { printf("COMM3\n"); }
//}

int main()
{
  int fd = open("test.txt", O_RDONLY, 0666);
  char outBuff[1024] = { 0 };
  ssize_t s = read(fd, outBuff, 1024);
  outBuff[s] = '\0';
  printf("%s", outBuff);

  close(fd);

  //umask(0); //umask系统调用:修改当前进程的umask
  //int fd = open("test.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //
  //char buff[1024] = "hello linux!\n";
  //
  //write(fd, buff, strlen(buff));

  ////write(1, buff, strlen(buff));

  //close(fd);
  //
  
  //pid_t id = fork();
  //if(id == 0)
  //{
  //  int fd = open("test22222.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //}
  //else if(id > 0)
  //{
  //  int status = 0;
  //  waitpid(id, &status, 0);
  //}
  //else
  //{
  //  perror("fork error"); exit(1);
  //}
  //getComm(COMM1 | COMM2 | COMM3);
  //printf("--------------------------\n");
  //getComm(COMM1 | COMM3);

  //chdir("/home/shr"); //如果修改了cwd，新建文件的位置也会被修改
  //FILE* pf = fopen("test.txt", "w");//如果文件不存在，则会在cwd新建文件
  //FILE* pf = fopen("test.txt", "a");
  //char buff[1024] = "hello linux!\n";
  ////char buff[1024] = "hello";
  //fwrite(buff, strlen(buff), 1, pf);
  //
  ////char readBuff[1024] = { 0 };
  //fread(readBuff, 1024, 1, pf);

  //fprintf(stdout, "%s", buff);

  //fclose(pf);
  return 0;
}
